﻿#include <iostream>
#include <hpdf.h>
#include <windows.h>
#include <locale>
#include <codecvt>
#include <vector>

// 错误处理函数
void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void* user_data) {
    std::cerr << "libharu error: error_no=" << error_no << ", detail_no=" << detail_no << std::endl;
}

int main() {
    HPDF_Doc pdf;
    HPDF_Page page;
    HPDF_Font font;
    HPDF_STATUS err;

    // 创建一个新的 PDF 文档
    pdf = HPDF_New(error_handler, nullptr);
    if (!pdf) {
        std::cerr << "Error: cannot create PDF object." << std::endl;
        return 1;
    }

    HPDF_UseCNSFonts(pdf);
    HPDF_UseCNTFonts(pdf);
    HPDF_UseCNTEncodings(pdf);
    HPDF_UseCNSEncodings(pdf);

    // 加载宋体字体
    font = HPDF_GetFont(pdf, "SimSun", "GBK-EUC-H"); // 官方文档说明：中文只支持GBK编码
    if (!font) {
        std::cerr << "Error: cannot load SimSun font." << std::endl;
        HPDF_Free(pdf);
        return 1;
    }

    // 创建第一页并初始化 page 指针
    page = HPDF_AddPage(pdf);
    HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
    HPDF_Page_BeginText(page);
    HPDF_Page_SetFontAndSize(page, font, 12);

    // 设置初始位置
    float margin_left = 200;
    float margin_top = HPDF_Page_GetHeight(page) - 50;
    HPDF_Page_MoveTextPos(page, margin_left, margin_top);

    int char_count = 0;
    int line_count = 0;

    for (unsigned char highByte = 0x81; highByte <= 0xA0; ++highByte) {
        for (unsigned char lowByte = 0x40; lowByte <= 0xFE; ++lowByte) {
            std::vector<unsigned char> gbkBytes = { highByte, lowByte };
            std::string gbkStr(gbkBytes.begin(), gbkBytes.end());
            if (char_count % 300 == 0 && char_count > 0) {
                // 创建新页面并保持相同的格式
                HPDF_Page_EndText(page);
                page = HPDF_AddPage(pdf);
                HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
                HPDF_Page_BeginText(page);
                HPDF_Page_SetFontAndSize(page, font, 12);
                HPDF_Page_MoveTextPos(page, margin_left, margin_top);  // 保持相同位置
                line_count = 0;
            }

            if (char_count % 15 == 0 && char_count > 0) {
                // 换行
                HPDF_Page_MoveTextPos(page, -15 * 12, -20);
                line_count++;
                margin_left = 388;
            }
            /*std::cout << gbkStr;
            std::cout << "(" << int(highByte) << ", " << int(lowByte) << ")";
            if (line_count == 25) {
                return 0;
            }*/
            if (lowByte != 127) {
                // 写入字符
                HPDF_Page_ShowText(page, gbkStr.c_str());
                HPDF_Page_MoveTextPos(page, 12, 0);  // 水平方向移动

                char_count++;
            }
        }
    }

    for (unsigned char highByte = 0xAA; highByte <= 0xAF; ++highByte) {
        for (unsigned char lowByte = 0x40; lowByte <= 0xA0; ++lowByte) {
            std::vector<unsigned char> gbkBytes = { highByte, lowByte };
            std::string gbkStr(gbkBytes.begin(), gbkBytes.end());
            if (char_count % 300 == 0 && char_count > 0) {
                // 创建新页面并保持相同的格式
                HPDF_Page_EndText(page);
                page = HPDF_AddPage(pdf);
                HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
                HPDF_Page_BeginText(page);
                HPDF_Page_SetFontAndSize(page, font, 12);
                HPDF_Page_MoveTextPos(page, margin_left, margin_top);  // 保持相同位置
                line_count = 0;
            }

            if (char_count % 15 == 0 && char_count > 0) {
                // 换行
                HPDF_Page_MoveTextPos(page, -15 * 12, -20);
                line_count++;
                margin_left = 388;
            }

            if (lowByte != 127) {
                // 写入字符
                HPDF_Page_ShowText(page, gbkStr.c_str());
                HPDF_Page_MoveTextPos(page, 12, 0);  // 水平方向移动

                char_count++;
            }
        }
    }

    for (unsigned char highByte = 0xB0; highByte <= 0xF7; ++highByte) {
        for (unsigned char lowByte = 0x40; lowByte <= 0xFE; ++lowByte) {
            std::vector<unsigned char> gbkBytes = { highByte, lowByte };
            std::string gbkStr(gbkBytes.begin(), gbkBytes.end());
            if (char_count % 300 == 0 && char_count > 0) {
                // 创建新页面并保持相同的格式
                HPDF_Page_EndText(page);
                page = HPDF_AddPage(pdf);
                HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
                HPDF_Page_BeginText(page);
                HPDF_Page_SetFontAndSize(page, font, 12);
                HPDF_Page_MoveTextPos(page, margin_left, margin_top);  // 保持相同位置
                line_count = 0;
            }

            if (char_count % 15 == 0 && char_count > 0) {
                // 换行
                HPDF_Page_MoveTextPos(page, -15 * 12, -20);
                line_count++;
                margin_left = 388;
            }

            if (lowByte != 127) {
                // 写入字符
                HPDF_Page_ShowText(page, gbkStr.c_str());
                HPDF_Page_MoveTextPos(page, 12, 0);  // 水平方向移动

                char_count++;
            }
        }
    }

    for (unsigned char highByte = 0xF8; highByte <= 0xFE; ++highByte) {
        for (unsigned char lowByte = 0x40; lowByte <= 0xA0; ++lowByte) {
            std::vector<unsigned char> gbkBytes = { highByte, lowByte };
            std::string gbkStr(gbkBytes.begin(), gbkBytes.end());
            if (char_count % 300 == 0 && char_count > 0) {
                // 创建新页面并保持相同的格式
                HPDF_Page_EndText(page);
                page = HPDF_AddPage(pdf);
                HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
                HPDF_Page_BeginText(page);
                HPDF_Page_SetFontAndSize(page, font, 12);
                HPDF_Page_MoveTextPos(page, margin_left, margin_top);  // 保持相同位置
                line_count = 0;
            }

            if (char_count % 15 == 0 && char_count > 0) {
                // 换行
                HPDF_Page_MoveTextPos(page, -15 * 12, -20);
                line_count++;
                margin_left = 388;
                std::cout << std::endl;
            }

            std::cout << gbkStr;
            std::cout << "(" << int(highByte) << ", " << int(lowByte) << ")";
            if (line_count == 25) {
                return 0;
            }

            if (lowByte != 127) {
                // 写入字符
                HPDF_Page_ShowText(page, gbkStr.c_str());
                HPDF_Page_MoveTextPos(page, 12, 0);  // 水平方向移动

                char_count++;
            }
        }
    }

    // 结束最后一页的文本写入
    HPDF_Page_EndText(page);

    // 保存 PDF 文件
    err = HPDF_SaveToFile(pdf, "simsun_characters.pdf");
    if (err) {
        std::cerr << "Error: cannot save PDF file." << std::endl;
    }

    // 释放 PDF 文档
    HPDF_Free(pdf);

    std::cout << "PDF file created successfully." << std::endl;

    return 0;
}
